package com.example.admindemo.controller;

import cn.hutool.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admindemo.entity.Result;
import com.example.admindemo.entity.User;
import com.example.admindemo.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {
    @Resource
    private UserMapper userMapper;

    @DeleteMapping
    public Result<?> delete(@RequestParam Integer id) {
        int i = userMapper.deleteById(id);
        return Result.success();
    }

    @PutMapping()
    public Result<?> update(@RequestBody User user) {
//        int insert = userMapper.insert(user);
//        System.out.println("user = " + user);
        int i = userMapper.updateById(user);
        return Result.success();
    }

    @PostMapping()
    public Result<?> save(@RequestBody User user) {
        int insert = userMapper.insert(user);
//        System.out.println("user = " + user);
        return Result.success();
    }


    @GetMapping("/checktoekn")
    public Result<?> check(String token) {
        System.out.println("token = " + token);
        if (token != null) {
            System.out.println("token = " + token);
            JWT jwt = JWT.create();
//        盐
            System.out.println("test");
            byte[] bytes = "123456".getBytes();
            boolean verify = JWT.of(token).setKey(bytes).verify();
            if (verify) {
                return Result.success();
            }
        }

        return Result.error("-1", "token验证失败");
    }

    @PostMapping("/login")
    public Result<?> loogin(@RequestBody User user) {
//        int insert = userMapper.insert(user);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername()).eq("password", user.getPassword());
        User user1 = userMapper.selectOne(wrapper);
        String sign = null;
        if (user1 != null) {
//            System.out.println("user = " + user);
            JWT jwt = JWT.create();
            jwt.setPayload("username", "许嵩");
            byte[] bytes = "123456".getBytes();
            jwt.setKey(bytes);
            System.out.println(jwt);
            sign = jwt.sign();
//            System.out.println("sign = " + sign);
            return Result.success(sign);
        }
        return Result.error("-1", "账号或密码错误");
//        return Result.success(sign);
    }

    @GetMapping()
    public Result<?> search(@RequestParam(defaultValue = "1") Integer sizePage, @RequestParam(defaultValue = "5") Integer PageNum, String search) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (search != null) {
            wrapper.like("nick_name", search);
        }
        Page<User> userPage = userMapper.selectPage(new Page<>(sizePage, PageNum), wrapper);
//        int insert = userMapper.insert(user);
//        System.out.println("user = " + user);
        return Result.success(userPage);
    }
}
